<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-title">
        <h2>用户注册</h2>
        <p class="subtitle">创建您的账号</p>
      </div>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" class="register-form">
        <el-form-item prop="phone">
          <el-input 
            v-model="registerForm.phone"
            placeholder="请输入手机号"
            :prefix-icon="Iphone"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="code" class="verify-code-item">
          <el-input 
            v-model="registerForm.code"
            placeholder="请输入验证码"
            :prefix-icon="Key"
            maxlength="6"
            type="number"
            size="large"
            @input="handleCodeInput"
          >
            <template #append>
              <el-button 
                :disabled="isCountingDown" 
                @click="sendVerifyCode"
                class="send-code-btn"
              >
                {{ countDownText }}
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            size="large"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            :prefix-icon="Lock"
            show-password
            size="large"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" class="register-button" size="large">
            注册
          </el-button>
        </el-form-item>
        <div class="login-link">
          已有账号？<router-link to="/login">立即登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { Iphone, Lock, Key } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const registerFormRef = ref(null)
const isCountingDown = ref(false)
const countdown = ref(60)
const countDownText = ref('发送验证码')

const registerForm = reactive({
  phone: '',
  code: '',
  password: '',
  confirmPassword: ''
})

const validatePhone = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请输入手机号'))
  } else if (!/^1[3-9]\d{9}$/.test(value)) {
    callback(new Error('请输入正确的手机号'))
  } else {
    callback()
  }
}

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,15}$/
    if (!passwordRegex.test(value)) {
      callback(new Error('密码必须包含大小写字母、数字和特殊符号(!@#$%^&*)，长度8-15位'))
    } else {
      if (registerForm.confirmPassword !== '') {
        registerFormRef.value.validateField('confirmPassword')
      }
      callback()
    }
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  phone: [
    { required: true, validator: validatePhone, trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 6, message: '验证码长度应为6位', trigger: 'blur' },
    { pattern: /^\d{6}$/, message: '验证码必须为6位数字', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ]
}

const startCountDown = () => {
  isCountingDown.value = true
  countdown.value = 60
  const timer = setInterval(() => {
    countdown.value--
    countDownText.value = `${countdown.value}秒后重试`
    if (countdown.value <= 0) {
      clearInterval(timer)
      isCountingDown.value = false
      countDownText.value = '发送验证码'
    }
  }, 1000)
}

const sendVerifyCode = () => {
  registerFormRef.value.validateField('phone', (valid) => {
    if (!valid) {
      // 这里添加发送验证码的实际逻辑
      const verifyCode = Math.floor(100000 + Math.random() * 900000).toString()
      console.log('验证码：', verifyCode) // 实际项目中应该通过短信发送
      ElMessage.success('验证码已发送')
      startCountDown()
    }
  })
}

const handleCodeInput = (value) => {
  // 限制只能输入6位数字
  if (value.length > 6) {
    registerForm.code = value.slice(0, 6)
  }
  // 移除非数字字符
  registerForm.code = registerForm.code.replace(/\D/g, '')
}

const handleRegister = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      // 这里添加实际的注册逻辑
      ElMessage.success('注册成功')
      router.push('/login')
    }
  })
}
</script>

<style scoped>
.register-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.9) 0%, rgba(118, 75, 162, 0.9) 100%);
  overflow: hidden;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  background-image: url('../assets/bg.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  z-index: -1;
  filter: brightness(0.8) opacity(0.8);
}

.register-box {
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 8px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  margin-right: 15%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 1;
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
}

.register-title h2 {
  color: #333;
  font-size: 24px;
  margin: 0;
  margin-bottom: 10px;
  font-weight: 600;
}

.subtitle {
  color: #666;
  font-size: 14px;
}

.register-form {
  width: 100%;
}

.register-form :deep(.el-form-item) {
  margin-bottom: 20px;
}

.register-form :deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.9);
  border: none;
  box-shadow: none;
  height: 45px;
  transition: all 0.3s ease;
}

.register-form :deep(.el-input__wrapper:hover),
.register-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #764ba2;
  background-color: #fff;
}

.verify-code-item :deep(.el-input-group__append) {
  padding: 0;
  background: none;
  border: none;
  margin-left: 10px;
}

.verify-code-item :deep(.el-input) {
  width: calc(100% - 120px);
}

.send-code-btn {
  width: 110px;
  height: 45px;
  padding: 0 15px;
  border: none;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  white-space: nowrap;
  position: absolute;
  right: -120px;
  top: 0;
}

.send-code-btn:not(:disabled):hover {
  opacity: 0.95;
}

.register-button {
  width: 100%;
  height: 45px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.register-button:hover {
  opacity: 0.95;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: #666;
  font-size: 14px;
}

.login-link a {
  color: #764ba2;
  text-decoration: none;
  font-weight: 500;
}

.login-link a:hover {
  text-decoration: underline;
}

@media (max-width: 1200px) {
  .register-box {
    margin-right: 10%;
  }
}

@media (max-width: 768px) {
  .register-box {
    margin-right: 5%;
  }
}

@media (max-width: 480px) {
  .register-box {
    margin: 0;
    height: 100%;
    max-width: 100%;
    border-radius: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: rgba(255, 255, 255, 0.98);
    overflow-y: auto;
  }
  
  .register-container {
    padding: 0;
  }
}
</style>
